package com.gimi.cloud.jimi.manage.service.log.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.MyBeanUtils;
import com.gimi.cloud.jimi.dao.dao.log.LogManageRequestDao;
import com.gimi.cloud.jimi.dto.log.LogManageRequestDTO;
import com.gimi.cloud.jimi.entity.log.LogManageRequestEntity;
import com.gimi.cloud.jimi.manage.service.log.LogManageRequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Manage接口访问日志相关接口
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-27
 **/
@Service
public class LogManageRequeustServiceImpl implements LogManageRequestService{
    private static final Logger logger = LoggerFactory.getLogger(LogManageRequeustServiceImpl.class);
    private LogManageRequestDao logManageRequestDao;

    @Autowired
    public LogManageRequeustServiceImpl(LogManageRequestDao logManageRequestDao) {
        this.logManageRequestDao = logManageRequestDao;
    }

    @Override
    public List<LogManageRequestDTO> queryList(LogManageRequestDTO dto, boolean isPaging) throws Exception {
        return null;
    }

    @Override
    public LogManageRequestDTO load(LogManageRequestDTO dto, Object id) throws Exception {
        LogManageRequestEntity logManageRequestEntity = logManageRequestDao.queryBy("re_id",id);
        if(null == logManageRequestEntity){
            return null;
        }
        LogManageRequestDTO logManageRequestDTO = new LogManageRequestDTO();
        MyBeanUtils.copyPropertiesIgnoreNull(logManageRequestEntity,logManageRequestDTO);
        return logManageRequestDTO;
    }

    @Override
    public LogManageRequestDTO saveOrUpdate(LogManageRequestDTO dto) throws Exception {
        LogManageRequestEntity logManageRequestEntity = null;
        if(null != dto.getReId()){
            logManageRequestEntity = logManageRequestDao.queryBy("re_id",dto.getReId());
            if(null == logManageRequestEntity){
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取主键为" + dto.getReId() + "的请求日志信息，修改失败");
            }
        }else{
            logManageRequestEntity = new LogManageRequestEntity();
        }
        MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,logManageRequestEntity);
        if(null != logManageRequestEntity.getReId()){
            logManageRequestDao.baseUpdateByPrimaryKey(logManageRequestEntity);
        }else{
            logManageRequestDao.baseInsert(logManageRequestEntity);
        }
        MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(logManageRequestEntity,dto);
        return dto;
    }

    @Override
    public int delete(LogManageRequestDTO dto, Object id, boolean realDel) throws Exception {
        return logManageRequestDao.deleteByPrimaryKey(id);
    }
}
